package com.hqhsc.module.front.activity.dao;

import com.hqhsc.core.dao.BaseJdbcDaoImpl;
import com.hqhsc.module.common.dao.CommonDao;
import com.hqhsc.module.front.activity.domain.Vote;
import com.hqhsc.module.front.activity.domain.VoteRowMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class VoteDao extends BaseJdbcDaoImpl {
	final Logger logger = LoggerFactory.getLogger(VoteDao.class);


    @Autowired
    private CommonDao commonDao;


    public List<Vote> getPorductList(long activity_id) {
        try{
            String sql = "select * from votes where activity_id = ? and status = 1";
            Object[] o = new Object[]{ activity_id };
            logger.info(sql.replaceAll("\\?", "{}"), o);
            return this.getJdbcTemplate().query(sql, o, new VoteRowMapper());
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
            return null;
        }
    }

    public Vote getProductById(long id) {
        try{
            String sql = "select * from votes where id = ? and status = 1";
            Object[] o = new Object[]{ id };
            logger.info(sql.replaceAll("\\?", "{}"), o);
            return this.getJdbcTemplate().queryForObject(sql, o, new VoteRowMapper());
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
            return null;
        }
    }

    public int doVote(long id) {
        String sql = "update votes set vote_num = vote_num + 1 where id = ? and status = 1";
        Object[] o = new Object[]{ id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

}
